package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.QuestionSelection;
import com.think.teaching.service.impl.QuestionSelectionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 选择题选项 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "选择题选项类型管理接口")
public class QuestionSelectionController {
	
	private final QuestionSelectionServiceImpl questionSelectionService;

	public QuestionSelectionController(QuestionSelectionServiceImpl questionSelectionService) {
		this.questionSelectionService = questionSelectionService;
	}

	@ApiOperation("添加新的选项")
	@PostMapping("admin/selection")
	public Result AddNewQuestionSelection(@RequestBody()QuestionSelection data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			QuestionSelection question = questionSelectionService.AddQuestionSelection(data);
			if (question != null) {
				return Result.success(question);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新选项")
	@RequestMapping(value = "admin/selection",method = RequestMethod.PUT)
	public Result UpdateQuestionSelection(@RequestBody()QuestionSelection data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			QuestionSelection question = questionSelectionService.UpdateQuestionSelection(data);
			if (question != null) {
				return Result.success(question);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除选项")
	@RequestMapping(value = "admin/selection/{id}",method = RequestMethod.DELETE)
	public Result DeleteQuestionSelection(@PathVariable("id")Integer id) {
		QuestionSelection question = questionSelectionService.getById(id);
		if (question == null) {
			return Result.failure(ResultCode.QUESTION_SELECTION_NOT_EXIST);
		} else {
			if (questionSelectionService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取选项详情")
	@RequestMapping(value = "admin/selection/{id}",method = RequestMethod.GET)
	public Result GetQuestionSelection(@PathVariable("id")Integer id) {
		QuestionSelection question = questionSelectionService.getById(id);
		if (question == null) {
			return Result.failure(ResultCode.QUESTION_SELECTION_NOT_EXIST);
		} else {

			return Result.success(question);
		}
	}
	
	@ApiOperation("管理员修改选择题选项状态")
	@RequestMapping(value = "admin/selection/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateQuestionSelectionState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		QuestionSelection question = questionSelectionService.getById(id);
		if (question == null) {
			return Result.failure(ResultCode.QUESTION_SELECTION_NOT_EXIST);
		} else {
			try {
				question = questionSelectionService.SetQuestionSelectionState(id, state);
				return Result.success(question);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
}

